﻿using System;
using System.Windows.Forms;
using System.IO;
using SeriesUtil.Helper;

namespace SeriesUtil
{
    public partial class SeriesUtilWindow : Form
    {
        private TextWriter writer;

        public SeriesUtilWindow()
        {
            InitializeComponent();
            // Console.writeLine() shall write to txtLog
            writer = new Log(txtLog);
            Console.SetOut(writer);

            // im pretty shure, there has to be a way to make the binding the "Microsoft-Way" but how?
            this.comboBoxProfiles.DataSource = SeriesUtil.Properties.Settings.Default.Profiles;
            this.comboBoxProfiles.DisplayMember = "Name";
            this.comboBoxProfiles.ValueMember = "Id";
        }

        private void SeriesUtilWindow_Load(object sender, EventArgs e)
        {   
            // get Settings and use them for initializing
            this.Size = Properties.Settings.Default.Size;
            // wont work... why?
            this.comboBoxProfiles.SelectedValue = Properties.Settings.Default.ProfileId;
          
            // test for List<ParserProfile>            
            /*
            List<ParserProfile> profiles = new List<ParserProfile>();
            ParserProfile profile = new ParserProfile("One Piece", true, "input", "output");
            profiles.Add(profile);
            Properties.Settings.Default.Profiles = profiles;
            Properties.Settings.Default.Save();

            Properties.Settings.Default.Reload();
            */
        }

        private void Browse_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                String path = folderBrowserDialog.SelectedPath;
                this.txtInputPath.Text = path;
                //new SeriesParser(path, checkBoxAbsolute.Checked).UpdateEpisodeListView(EpisodeListView);
            }
        }

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SettingsWindow options = new SettingsWindow();
            options.ShowDialog();
        }

        private void infoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutWindow about = new AboutWindow();
            about.ShowDialog();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                String path = folderBrowserDialog.SelectedPath;
                this.txtInputPath.Text = path;
            }
        }

        private void SeriesUtilWindow_FormClosing(object sender, FormClosingEventArgs e)
        {
            // save Settings
            // TODO: prevent from NullReferenceExceptions...
            Properties.Settings.Default.Size = this.Size;
            Properties.Settings.Default.Location = this.Location;
            Properties.Settings.Default.Path = this.txtInputPath.Text;
            //Properties.Settings.Default.ProfileId = this.comboBoxProfiles.SelectedValue.ToString();
            Properties.Settings.Default.Save();
        }
    }
}
